import axios from "axios";
import { notification } from "antd";
import { createBrowserHistory } from 'history'

var history = createBrowserHistory()
//实例化对象
var service = axios.create({
    baseURL:'',
    timeout:2000
})
//设置拦截器
service.interceptors.request.use(
    // (res)=>{
    //     if(res.data.code = 10119){
    //         localStorage.removeItem('token')
    //         history.push('/login')
    //     }
    //     return res;
    // },
    (config)=>{
        //携带请求头
        config.headers['token'] = localStorage.getItem('token'); 
        return config;

    },
    (error)=>{
        return Promise.reject(error)
    }
)

service.interceptors.response.use(
    (config)=>{
        return config

    },
    (error)=>{
        if( error.response.status == 401 ){
            notification.error({ message:'登陆过期!' });
            //跳转到登录页
            //删除token
        }
        else if( error.response.status == 404 ){
            notification.error({ message:'访问路径有误!' });
        }
        else if( error.response.status == 500 ){
            notification.error({ message:'服务器内部错误!' });
        }

        return Promise.reject(error)
    }
)
export default service;
